package cn.iocoder.yudao.module.system.controller.app.tenantdetail;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.system.controller.admin.tenantdetail.vo.TenantDetailCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tenantdetail.vo.TenantDetailPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tenantdetail.vo.TenantDetailRespVO;
import cn.iocoder.yudao.module.system.controller.app.tenantdetail.vo.AppTenantDetailCheckReqVO;
import cn.iocoder.yudao.module.system.controller.app.tenantdetail.vo.AppTenantDetailCheckRespVO;
import cn.iocoder.yudao.module.system.convert.tenantdetail.TenantDetailConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.tenantdetail.TenantDetailDO;
import cn.iocoder.yudao.module.system.dal.dataobject.userdetail.UserDetailDO;
import cn.iocoder.yudao.module.system.service.tenantdetail.TenantDetailService;
import cn.iocoder.yudao.module.system.service.userdetail.UserDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.SimpleTrigger;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 租户详情")
@RestController
@RequestMapping("/system/tenant")
@Validated
public class AppTenantDetailController {

    @Resource
    private TenantDetailService tenantDetailService;

    @Resource
    private UserDetailService userDetailService;


    @PostMapping("/check")
    @Operation(summary = "检查给定手机号是否有大愚灸舍")
    public CommonResult<AppTenantDetailCheckRespVO> checkTenantDetail(@Valid @RequestBody AppTenantDetailCheckReqVO reqVO) {
        Set<String> mobileList = new HashSet<>();
        for(int i = 0; i<reqVO.getMobileList().size(); i++){
            mobileList.add(reqVO.getMobileList().get(i));
        }

        List<TenantDetailDO> tenantDetailDOList = tenantDetailService.getTenantDetailListbyMobile(mobileList);

        return success(TenantDetailConvert.INSTANCE.convertAppCheck(tenantDetailDOList));
    }

    @GetMapping("/get")
    @Parameter(name = "mobile", description = "手机号码", required = true, example = "186")
    @Operation(summary = "获取大愚灸舍详情")
    public CommonResult<TenantDetailRespVO> getTenantDetail(@RequestParam("mobile") String mobile){
        return success(TenantDetailConvert.INSTANCE.convert(tenantDetailService.getTenantDetailByMobile(mobile)));
    }

    @GetMapping("/page")
    @Operation(summary = "获得灸舍详情分页")
    public CommonResult<PageResult<TenantDetailRespVO>> getTenantDetailPage(@Valid TenantDetailPageReqVO reqVO){
        PageResult<TenantDetailDO> pageResult= tenantDetailService.getTenantDetailPage(reqVO);

        Set<String> mobileList = CollectionUtils.convertSet(pageResult.getList(), TenantDetailDO::getMobile);
        List<UserDetailDO> userList = userDetailService.getUserDetailByMobiles(mobileList);

        Map<String, UserDetailDO> userMap = CollectionUtils.convertMap(userList, UserDetailDO::getMobile);

        return success(TenantDetailConvert.INSTANCE.convertPage02(pageResult, userMap));
    }
}
